package com.netease.nim.demo.session.viewholder;

import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.netease.nim.demo.common.adapter.TAdapter;
import com.netease.nim.demo.common.media.audioplayer.Playable;
import com.netease.nim.demo.session.audio.MessageAudioControl;
import com.netease.nim.demo.util.TimeUtil;
import com.netease.nimlib.sdk.msg.attachment.AudioAttachment;
import com.netease.nimlib.sdk.msg.constant.AttachStatusEnum;
import com.netease.nimlib.sdk.msg.constant.MsgStatusEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.shwread.android.qysw10000204.R;

public class MsgViewHolderAudioLeft extends MsgViewHolder {

	public TextView audioLength;
	public View playAudioButton;
	private ImageView statusImageView;
	private View statusLayout;
	private MessageAudioControl mBaseAudioControl;
	private ImageView audioImageView;
	private IMMessage message;
	private ProgressBar progress;
	private LinearLayout playAudioProgressBar;

	@Override
	protected int getResId() {
		return R.layout.audio_message_view_left_item;
	}

	@Override
	public void inflate() {
		super.inflate();

		audioLength = (TextView) view.findViewById(R.id.textViewAudioLength);
		playAudioButton = (View) view.findViewById(R.id.buttonViewStartPlayAudio);
		statusImageView = (ImageView) view.findViewById(R.id.imageViewStatus);
		statusLayout = (View) view.findViewById(R.id.layoutStatus);
		playAudioProgressBar = (LinearLayout) view.findViewById(R.id.layoutPlayAudio);
		audioImageView = (ImageView) view.findViewById(R.id.imageViewAudio);
		progress = (ProgressBar) view.findViewById(R.id.msg_progressBar);

		longClickView = playAudioButton;

		mBaseAudioControl = MessageAudioControl.getInstance(context);
	}

	@Override
	public void refresh(Object item) {
		super.refresh(item);

		message = messageItem.getMessage();
		final AudioAttachment msgAttachment = (AudioAttachment) message.getAttachment();
		long milliseconds = msgAttachment.getDuration();
		setAudioBubbleWidth(milliseconds);

		// 消息状态
        AttachStatusEnum status = message.getAttachStatus();
		switch (status) {
		case transferring:
			progress.setVisibility(View.VISIBLE);
			alertButton.setVisibility(View.GONE);
			statusLayout.setVisibility(View.GONE);
			break;
		case transferred:
			progress.setVisibility(View.GONE);
			alertButton.setVisibility(View.GONE);

			statusLayout.setVisibility(View.VISIBLE);
			if (message.getStatus() == MsgStatusEnum.read) {
				statusImageView.setVisibility(View.GONE);
			} else {
				statusImageView.setVisibility(View.VISIBLE);
			}
			audioLength.setVisibility(View.VISIBLE);

			String audioPath = msgAttachment.getPath();
			if (TextUtils.isEmpty(audioPath)) {
				// TODO : redownload();
			}
			break;
		case fail:
			progress.setVisibility(View.GONE);
			alertButton.setVisibility(View.VISIBLE);
			statusLayout.setVisibility(View.GONE);
			break;
		default:
			progress.setVisibility(View.GONE);
			alertButton.setVisibility(View.GONE);
			statusLayout.setVisibility(View.GONE);
			break;
		}
		
		if (!isMessagePlaying(mBaseAudioControl, message)) {
			if (mBaseAudioControl.getAudioControlListener() != null
					&& mBaseAudioControl.getAudioControlListener().equals(onPlayListener)) {
				mBaseAudioControl.changeAudioControlListener(null);
			}

			setAudioLength(milliseconds);
			audioImageView.setVisibility(View.VISIBLE);
			playAudioProgressBar.setVisibility(View.GONE);
			for (int i = 0; i < playAudioProgressBar.getChildCount(); i++) {
				View view = playAudioProgressBar.getChildAt(i);
				AnimationDrawable animationDrawable = (AnimationDrawable) view.getBackground();
				stopAnimation(animationDrawable, playAudioButton, true);
			}
		} else {
			mBaseAudioControl.changeAudioControlListener(onPlayListener);

			audioImageView.setVisibility(View.GONE);
			playAudioProgressBar.setVisibility(View.VISIBLE);
			for (int i = 0; i < playAudioProgressBar.getChildCount(); i++) {
				View view = playAudioProgressBar.getChildAt(i);
				AnimationDrawable animationDrawable = (AnimationDrawable) view.getBackground();
				startAnimation(animationDrawable, playAudioButton, true);
			}
		}

		playAudioButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mBaseAudioControl != null) {
					if (message.getAttachStatus() != AttachStatusEnum.transferred) {
						// todo 未下载完毕或者消息错误时
						return;
					}

					if (message.getStatus() != MsgStatusEnum.read) {
						// 将未读标识去掉,更新数据库
						statusImageView.setVisibility(View.GONE);
					}

					mBaseAudioControl.startPlayAudioDelay(CLICK_TOPLAY_AUDIO_DELAY, message, onPlayListener);
					mBaseAudioControl.setPlayNext(true, (TAdapter) adapter, messageItem);
				}
			}
		});
	}

	private void setAudioLength(long milliseconds) {
		long seconds = TimeUtil.getSecondsByMilliseconds(milliseconds);

		if (seconds >= 1) {
			audioLength.setText(seconds + "\"");
		} else {
			audioLength.setText("");
		}
	}

	private void setAudioBubbleWidth(long milliseconds) {
		long seconds = TimeUtil.getSecondsByMilliseconds(milliseconds);

		int currentBubbleWidth = calculateBubbleWidth(seconds, MAX_AUDIO_TIME_SECOND);
		LayoutParams layoutParams = playAudioButton.getLayoutParams();
		layoutParams.width = currentBubbleWidth;
		playAudioButton.setLayoutParams(layoutParams);
	}

	private MessageAudioControl.AudioControlListener onPlayListener = new MessageAudioControl.AudioControlListener() {

		@Override
		public void updatePlayingProgress(Playable playable, long curPosition) {
			long seconds = curPosition;
			if (seconds > playable.getDuration()) {
				return;
			}
			setAudioLength(seconds);
		}

		@Override
		public void onAudioControllerReady(Playable playable) {
			if (eventListener != null) {
				eventListener.onAudioStartPlay();
			}

			audioImageView.setVisibility(View.GONE);
			playAudioProgressBar.setVisibility(View.VISIBLE);
			for (int i = 0; i < playAudioProgressBar.getChildCount(); i++) {
				View view = playAudioProgressBar.getChildAt(i);
				AnimationDrawable animationDrawable = (AnimationDrawable) view.getBackground();
				startAnimation(animationDrawable, playAudioButton, true);
			}
		}

		@Override
		public void onEndPlay(Playable playable) {
			if (eventListener != null) {
				eventListener.onAudioEndPlay();
			}

			setAudioLength(playable.getDuration());

			audioImageView.setVisibility(View.VISIBLE);
			playAudioProgressBar.setVisibility(View.GONE);
			for (int i = 0; i < playAudioProgressBar.getChildCount(); i++) {
				View view = playAudioProgressBar.getChildAt(i);
				AnimationDrawable animationDrawable = (AnimationDrawable) view.getBackground();
				stopAnimation(animationDrawable, playAudioButton, true);
			}
		}
	};

	@Override
	public void reclaim() {
		super.reclaim();
		if (mBaseAudioControl.getAudioControlListener() != null
				&& mBaseAudioControl.getAudioControlListener().equals(onPlayListener)) {
			mBaseAudioControl.changeAudioControlListener(null);
		}
	}

	@Override
	protected int getViewHolderDirection() {
		return VIEW_HOLDER_LEFT;
	}

}
